//
//  CashDetailViewController.m
//  MyNewProject
//
//  Created by sun on 2019/3/13.
//  Copyright © 2019 sun. All rights reserved.
//

#import "CashDetailViewController.h"

@interface CashDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView * myTalbleView;
@property(nonatomic,strong)NSMutableArray * myData;
@property(nonatomic,assign)NSInteger page;

@end

@implementation CashDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.gk_navTitle = @"提现记录";
    self.myData = [NSMutableArray array];
    
    self.page = 1;
    [self.view addSubview:self.myTalbleView];
    
    [self.myTalbleView.mj_header beginRefreshing];
    
}
-(void)loadData
{
    NSString * url = @"my/getTixian";
    NSDictionary * dic =  dic = @{@"uid":self.uid,@"loginKey":self.loginKey,@"page":[NSString stringWithFormat:@"%ld",(long)self.page]};;
   
    SKWeakSelf;
    
    [YQNetworking postWithUrl:url params:dic reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        
        [weakSelf.myTalbleView.mj_header endRefreshing];
        [weakSelf.myTalbleView.mj_footer endRefreshing];
        
        if ([response[@"status"] isEqualToString:@"ok"]||[response[@"status"] isEqualToString:@"empty"]) {
            
            if (weakSelf.page == 1) {
                [weakSelf.myData removeAllObjects];
                
            }
            if ([response[@"data"] count] >0) {
                [self.myData addObjectsFromArray:response[@"data"]];
                
            }
            else
            {
                [weakSelf.myTalbleView.mj_footer endRefreshingWithNoMoreData];
                [weakSelf.view makeToast:@"没有更多数据" duration:1 position: CSToastPositionCenter];
            }
            [weakSelf.myTalbleView reloadData];
            
            
        }
        else
        {
            
            [weakSelf.view makeToast:SafeString(response[@"msg"]) duration:1 position: CSToastPositionCenter];
            
        }

        
        
    } failBlock:^(NSError *error) {
        [weakSelf.myTalbleView.mj_header endRefreshing];
        [weakSelf.myTalbleView.mj_footer endRefreshing];
        
        [weakSelf.view makeToast:SafeString(@"加载失败") duration:1 position: CSToastPositionCenter];
        
    }];
    
}
-(UITableView*)myTalbleView
{
    
    if (_myTalbleView ==nil) {
        _myTalbleView =[[UITableView alloc] initWithFrame:CGRectMake(0,SKTopBar_Height ,SCREEN_WIDTH , SCREENH_HEIGHT-SKTopBar_Height) style:UITableViewStyleGrouped];
        _myTalbleView.dataSource = self;
        _myTalbleView.delegate = self;
        _myTalbleView.estimatedRowHeight = 0;
        _myTalbleView.estimatedSectionFooterHeight = 0;
        _myTalbleView.estimatedSectionHeaderHeight = 0;
        _myTalbleView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (@available(iOS 11.0, *)) {
            
            _myTalbleView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
            
        }
        
        SKWeakSelf;
        _myTalbleView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            weakSelf.page  = 1;
            if (weakSelf.myData.count>0) {
                [weakSelf.myTalbleView.mj_footer resetNoMoreData];
                
            }
            
            [weakSelf loadData];
        }];
        
        _myTalbleView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            weakSelf.page ++;
            
            [weakSelf loadData];
            
            
        }];
        _myTalbleView.mj_footer.automaticallyHidden = YES;
        
    }
    return _myTalbleView;
}

#pragma mark -  UITableView 代理相关

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numberOfRows = _myData.count;
    
    return numberOfRows;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    
    for (id view in [cell.contentView subviews])
    {
        
        [view removeFromSuperview];
        
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, 200, 25)];
    titleLab.textColor = SKColorWithHex(0x444444);
    titleLab.font = SKFONT_Semibold(20.0f);
    titleLab.textAlignment = NSTextAlignmentLeft;
    [cell.contentView addSubview:titleLab];
    
    
    UILabel * timeLab = [[UILabel alloc] initWithFrame:CGRectMake(20, titleLab.bottom, 200, 25)];
    timeLab.textColor = SKColorWithHex(0x999999);
    timeLab.font = SKFONT_Semibold(14.0f);
    timeLab.textAlignment = NSTextAlignmentLeft;
    [cell.contentView addSubview:timeLab];
    
    
    UILabel * priceLab = [[UILabel alloc] initWithFrame:CGRectMake(timeLab.right +15 , titleLab.bottom, SCREEN_WIDTH - timeLab.right -15 -20, 25)];
    
    priceLab.font = SKFONT_Medium(18);
    priceLab.textAlignment = NSTextAlignmentRight;
    [cell.contentView addSubview:priceLab];
    
    
    UIImageView * imageView =[[UIImageView alloc] initWithFrame:CGRectMake(0, priceLab.bottom +15-.5, SCREEN_WIDTH, 0.5)];
    imageView.backgroundColor =SKColorWithHex(0xE6E6E6);
    
     if (_myData.count>0) {
        [cell.contentView addSubview:imageView];

        NSDictionary * flData = [_myData objectAtIndex:indexPath.row];
        //
        timeLab.text = SafeString(flData[@"time"]);

        switch ([flData[@"status"] intValue]) {
            case 0:
            {
                titleLab.text =[NSString stringWithFormat:@"%@",SafeString(flData[@"money"])] ;
                priceLab.textColor = SKColorWithHex(0x20A40E);

                priceLab.text = @"提现中";

            }
                break;
            case 1:
            {
                titleLab.text = [NSString stringWithFormat:@"%@",SafeString(flData[@"money"])];
                priceLab.textColor = SKColorWithHex(0x888888);
                priceLab.text =@"提现成功" ;

            }
                break;

            case 2:
            {
                titleLab.text = [NSString stringWithFormat:@"%@",SafeString(flData[@"money"])];
                priceLab.textColor = SKColorWithHex(0xFF2424);
                priceLab.text = @"提现失败" ;
                
            }
                break;



            default:
                break;
        }

    }
    
    
    return cell;
    
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 80;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    return nil;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return CGFLOAT_MIN;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


@end
